package com.hugh.commom.zk.clientbase.curator;

import com.alibaba.fastjson.JSON;
import com.hugh.commom.zk.client.entity.ZkNodeEntity;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;

import java.util.Objects;

public class CreateNote_curator {

    // 创建会话
    public static void main(String[] args) throws Exception {


        //不使用fluent编程风格

        RetryPolicy exponentialBackoffRetry = new ExponentialBackoffRetry(1000, 3);

        // 使用fluent编程风格
        CuratorFramework client = CuratorFrameworkFactory.builder()
                .connectString("127.0.0.1:2181")
                .sessionTimeoutMs(50000)
                .connectionTimeoutMs(30000)
                .retryPolicy(exponentialBackoffRetry)
                .namespace("base")  // 独立的命名空间 /base
                .build();

        client.start();

        System.out.println("会话2创建了");

        ZkNodeEntity node = ZkNodeEntity.builder().ip("123456").port(8888).rt(0L).build();
        // 创建节点
        String path = "/lg/c1";
        String s = client.create().creatingParentsIfNeeded()
                .withMode(CreateMode.EPHEMERAL).forPath(path, JSON.toJSONString(node).getBytes());

        System.out.println("节点递归创建成功，该节点路径" + s);

        Stat stat = client.checkExists().forPath(path);
        ZkNodeEntity node2 = ZkNodeEntity.builder().ip("12314").port(7777).rt(0L).build();

        int version = client.setData().forPath(path, JSON.toJSONString(node2).getBytes()).getVersion();
        if(Objects.nonNull(version)){

        }
    }


}
